package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.constant.MessageConstant;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.MenuService;
import com.itheima.health.service.PermissionService;
import com.itheima.health.service.RoleService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    @GetMapping("/findAll")
    public Result findAll() {
        List<Menu> menuList = menuService.findAll();
        return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuList);
    }

    @GetMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(int roleId) {
        List<Integer> menuIds = menuService.findMenuIdsByRoleId(roleId);
        return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuIds);
    }

    /**
     * 查询所有菜单信息,并以树形结构封装
     * @return
     */
    @GetMapping("/findAllMenuAsTree")
    public Result findAllMenuAsTree() {
        List<Menu> menuList = menuService.findAllMenuAsTree();
        return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menuList);
    }

    /**
     * 查询所有菜单信息,并以树形结构封装
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Menu menu) {
        menuService.add(menu);
        return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
    }

    /**
     * 根据id查询菜单信息
     * @return
     */
    @GetMapping("/findById")
    public Result findById(int id) {
        Menu menu = menuService.findById(id);
        return new Result(true, MessageConstant.QUERY_MENU_SUCCESS, menu);
    }

    /**
     * 根据id查询菜单信息
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody Menu menu) {
        menuService.update(menu);
        return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
    }

    /**
     * 根据id删除菜单信息
     * @return
     */
    @PostMapping("/deleteById")
    public Result deleteById(int id) {
        menuService.deleteById(id);
        return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
    }
}
